package com.syp.auth.common;

import java.util.Random;

public abstract class VCGen {

	public static enum ChrLib {

		/**
		 * 全字符集
		 */
		FULL(1, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
		/**
		 * 友好字符集，仅包含方便辨认的字符集
		 */
		FRIEDLY(1, "23456789abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ"),
		/**
		 * 数字字符集
		 */
		NUMBERIC(1, "0123456789")

		;

		private final int code;
		private final String lib;

		private ChrLib(int code, String lib) {
			this.code = code;
			this.lib = lib;
		}

		public int getCode() {
			return code;
		}

		public String getLib() {
			return lib;
		}
	}

	/**
	 * 生成验证码
	 * 
	 * @param len
	 * @return
	 */
	public static String generate(int len, ChrLib cl) {
		Random rand = new Random(System.currentTimeMillis());
		StringBuilder verifyCode = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			verifyCode.append(cl.getLib().charAt(rand.nextInt(cl.getLib().length() - 1)));
		}
		return verifyCode.toString();
	}

}
